package com.frame.auth.mvc.service.impl;

import com.frame.auth.constant.enums.AuthGrantTypeEnum;
import com.frame.auth.constant.TokenConstants;
import com.frame.auth.model.ClientReq;
import com.frame.auth.mvc.dao.mapper.CloudAuthClientMapper;
import com.frame.auth.mvc.dao.pojo.CloudAuthClient;
import com.frame.auth.mvc.service.ClientService;
import com.frame.common.tools.util.MDFiveUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @auther zhangwj
 * @date 2021/3/24 下午3:29
 */
@Service(value = "clientService")
public class ClientServiceImpl implements ClientService {

    @Resource
    private CloudAuthClientMapper cloudAuthClientMapper;

    @Override
    public void createClient(ClientReq clientReq) {
        String appKey = MDFiveUtils.getOnlyId();
        String appSecret = MDFiveUtils.getOnlyId();
        CloudAuthClient cloudAuthClient = new CloudAuthClient();
        cloudAuthClient.setAppKey(appKey);
        cloudAuthClient.setAppSecret(appSecret);
        cloudAuthClient.setAppName(clientReq.getAppName());
        cloudAuthClient.setAppLogo(clientReq.getAppLogo());
        cloudAuthClient.setAppDesc(clientReq.getAppDesc());
        cloudAuthClient.setCallback(clientReq.getCallBack());
        cloudAuthClient.setGrantTypes(AuthGrantTypeEnum.defaultType());
        cloudAuthClient.setAuthScope(null);
        cloudAuthClient.setAutoApprove("false");
        cloudAuthClient.setAccessTokenValidity(TokenConstants.ACCESS_TOKEN_VALIDITY);
        cloudAuthClient.setRefreshTokenValidity(TokenConstants.REFRESH_TOKEN_VALIDITY);
        cloudAuthClient.setCreateTime(new Date());
        cloudAuthClient.setUpdateTime(new Date());
        cloudAuthClientMapper.insertSelective(cloudAuthClient);
    }
}
